package top.hmtools.autoConfiguration;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;

import top.hmtools.common.StaticContent;

/**
 * 本工具包所需要使用的数据源、mybatis配置
 * @author HyboJ
 *
 */
@Configuration
@ConditionalOnProperty(prefix=StaticContent.CONFIG_PREFIX,value=StaticContent.CONFIG_ITEM_KEY_NAME_ENABLED,matchIfMissing=true)
// @MapperScan(basePackages = {"top.hmtools.rlDao"}, sqlSessionFactoryRef = StaticContent.SQL_SESSION_FACTORY_BEAN_NAME)
public class DatasourceMybatisConfiguration implements ApplicationContextAware {
	
	protected final Logger logger = LoggerFactory.getLogger(DatasourceMybatisConfiguration.class);
	
	/**
	 * spring 上下文
	 */
	private ApplicationContext applicationContext;

	/**
	 * 当前工具包所使用的数据源
	 */
	private DataSource dataSource;

	/**
	 * 实例化一个数据源
	 * 
	 * @return
	 */
	@Bean(name = StaticContent.DATASOURCE_BEAN_NAME)
	@ConfigurationProperties(prefix = StaticContent.CONFIG_PREFIX + "_datasource")
	@ConditionalOnProperty(prefix = StaticContent.CONFIG_PREFIX + "_datasource", value = StaticContent.CONFIG_ITEM_KEY_NAME_IS_ENABLED_RL_DATASOURCE, matchIfMissing = false)
	public DataSource rlDataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();
		logger.info("########## DatasourceMybatisConfiguration datasource's hashcode is ###########"+druidDataSource.hashCode());
		return druidDataSource;
	}

	/**
	 * 获取供本工具包使用的数据源
	 * 
	 * @return
	 */
	public DataSource getDataSource() {
		if (this.dataSource == null) {
			// 获取数据源
			String[] datasourceNames = this.applicationContext.getBeanNamesForType(DataSource.class);
			for (String dsName : datasourceNames) {
				this.dataSource = (DataSource) this.applicationContext.getBean(dsName);
				// 如果所有的数据源中有本工程生成的名为“rlDataSource”(top.hmtools.common.StaticContent.DATASOURCE_BEAN_NAME配置)实例，则使用之
				if (StaticContent.DATASOURCE_BEAN_NAME.equalsIgnoreCase(dsName)) {
					break;
				}
			}
		}
		logger.info("############################ datasource's hashcode is："+this.dataSource.hashCode());
		return this.dataSource;
	}

	/**
	 * TODO 会与使用本工具jar包的主工程bean重复 冲突
	 * 获取mybatis的核心 SqlSessionFactory
	 * 
	 * @return
	 * @throws Exception
	 */
//	@Bean(name = StaticContent.SQL_SESSION_FACTORY_BEAN_NAME)
//	public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
//
//		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//		sqlSessionFactoryBean.setDataSource(this.getDataSource());
//
//		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//
//		sqlSessionFactoryBean.setMapperLocations(resolver
//				.getResources("classpath:rl_mapper/*.xml"));
//		return sqlSessionFactoryBean.getObject();
//	}

	/**
	 * TODO 会与使用本工具jar包的主工程bean重复 冲突
	 * 获取SqlSessionTemplate
	 * 
	 * @return
	 */
//	@Bean(name=StaticContent.SQL_SESSION_TEMPLATE_BEAN_NAME)
//	public SqlSessionTemplate sqlSessionTemplate() {
//		SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) this.applicationContext.getBean(StaticContent.SQL_SESSION_FACTORY_BEAN_NAME);
//		return new SqlSessionTemplate(sqlSessionFactory);
//	}

	/**
	 * 从spring框架中获取上下文，并写入本类类变量
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

}
